package com.oh.client;

import com.oh.initializer.ProtocolClientInitializer;
import com.oh.core.common.constant.CommandEnum;
import com.oh.core.common.constant.SerializationAlgorithm;
import com.oh.core.common.utils.MessageUtils;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * 模拟登录和发送消息
 */
public class ProtocolClient {

    private final String host;

    private final int port;

    public ProtocolClient(String host, int port){
        this.host = host;
        this.port = port;
    }

    public void run(){
        EventLoopGroup eventGroup = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventGroup)
                    .channel(NioSocketChannel.class)
                    .handler(new ProtocolClientInitializer());
            ChannelFuture channelFuture = bootstrap.connect(host, port).sync();
            String targetId = channelFuture.channel().id().asLongText();
            System.out.println(targetId);
            String context = "古之学者必有师。师者，所以传道受业解惑也。人非生而知之者，孰能无惑？惑而不从师，其为惑也，终不解矣。" +
                    "生乎吾前，其闻道也固先乎吾，吾从而师之；生乎吾后，其闻道也亦先乎吾，吾从而师之。吾师道也，夫庸知其年之先后生于吾乎？" +
                    "是故无贵无贱，无长无少，道之所存，师之所存也。嗟乎！师道之不传也久矣！欲人之无惑也难矣！古之圣人，其出人也远矣，犹且从师而问焉；" +
                    "今之众人，其下圣人也亦远矣，而耻学于师。是故圣益圣，愚益愚。圣人之所以为圣，愚人之所以为愚，其皆出于此乎？爱其子，择师而教之；" +
                    "于其身也，则耻师焉，惑矣。彼童子之师，授之书而习其句读者，非吾所谓传其道解其惑者也。句读之不知，惑之不解，或师焉，或不焉，" +
                    "小学而大遗，吾未见其明也。巫医乐师百工之人，不耻相师。士大夫之族，曰师曰弟子云者，则群聚而笑之。问之，则曰：“彼与彼年相若也，" +
                    "道相似也。位卑则足羞，官盛则近谀。”呜呼！师道之不复可知矣。巫医乐师百工之人，君子不齿，今其智乃反不能及，其可怪也欤！圣人无常师。" +
                    "孔子师郯子、苌弘、师襄、老聃。郯子之徒，其贤不及孔子。孔子曰：三人行，则必有我师。是故弟子不必不如师，师不必贤于弟子，闻道有先后，" +
                    "术业有专攻，如是而已。李氏子蟠，年十七，好古文，六艺经传皆通习之，不拘于时，学于余。余嘉其能行古道，作《师说》以贻之。";
            int magic = 5710;
            for(int i = 0; i < 2000; i++){
                System.out.println(i);
                if(i == 1){
                    String str = "{\"userName\":\"whx\",\"password\":\"123456\"}";
                    channelFuture.channel().writeAndFlush(MessageUtils.splicingMsg(CommandEnum.LOGIN_REQUEST.getCommand(),magic, str.getBytes()));
                }else{
                    if(i == 6){
                        try {
                            Thread.sleep(1000*3);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    String str = context + i;
                    channelFuture.channel().writeAndFlush(MessageUtils.splicingMsg(CommandEnum.MESSAGE_REQUEST.getCommand(), magic, str.getBytes()));
                }
            }
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            eventGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) {
        new ProtocolClient("127.0.0.1",5710).run();
    }
}
